/*
 * 文件名：DateUtil.java
 * 版权：Copyright 2006-2011 AsiaInfo Tech. Co. Ltd. All Rights Reserved. 
 * 描述： DateUtil.java
 * 修改人：齐鹏飞
 * 修改时间：2011-8-12
 * 修改内容：新增
 */
package com.fengxing.ams.pub.util;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.time.DateFormatUtils;

/**
 * Date工具类
 * <p>
 * <p>
 * 
 * <pre>
 * </pre>
 * 
 * @author 齐鹏飞
 * @version CTMS V100R001 2011-8-12
 * @since CTMS V100R001C01
 */
public class DateUtil
{
    /**
     * 
     * 获得当前时间long类型。
     * 
     * @return long
     */
    public static long getCurrTimeLong()
    {
        return System.currentTimeMillis();
    }

    /**
     * 
     * 获得当前时间Date类型。
     * 
     * @return date
     */
    public static Date getCurrDate()
    {
        return new Date();
    }

    /**
     * 
     * 获得当前时间的Timestamp类型。
     * 
     * @return Timestamp
     */
    public static Timestamp getCurrTimestamp()
    {
        return new Timestamp(DateUtil.getCurrTimeLong());
    }

    /**
     * 
     * 从毫秒数 转换为“yyyyMMddHHmmss” 时间格式的字符串
     * 
     * @author longting
     * 
     * @param millis
     *            毫秒数
     * @param regex
     *            格式为yyyyMMddHHmmss正则表达式字符串
     * @return String
     * 
     */
    public static String formatCustomDateStr(long millis, String regex)
    {

        String startTime = DateFormatUtils.format(millis, regex);

        return startTime;

    }
    
    /**
     * 将形如Mon May 20 00:00:00 CST 2013的日期转换为yyyyMMddHHmmss格式
     * 用在前台页面时间框传值到后台sql
     * @param date
     * @return
     */
    public static String formatDate(Date date){
    	return DateFormatUtils.format(date, "yyyyMMddHHmmss");
    }
    
    
    /**
     * 将形如Mon May 20 00:00:00 CST 2013的日期转换为yyyyMMddHHmmss格式
     * 用在前台页面时间框传值到后台sql ，后六位为235959
     * @param date
     * @return
     */
    public static String formatEndDate(Date date){
    	StringBuffer sb = new StringBuffer( DateFormatUtils.format(date, "yyyyMMddHHmmss"));
    	
    	return sb.delete(8, sb.length()).append("235959").toString();
    }

    /**
     * 
     * 获取处理时长方法(精确到分钟)
     * 
     * @param createdDate
     * @return
     */
    public static long getOperateTime(Date createdDate, Date endDate)
    {
        if (createdDate == null)
        {
            return 0;
        }

        Date date = new Date();
        if (endDate != null)
        {
            date = endDate;
        }
        if (createdDate.getTime() > date.getTime())
        {
            return 0;
        }
        long diff = date.getTime() - createdDate.getTime();
        Long returnMinute = ((diff / (60 * 1000)) - diff
                / (24 * 60 * 60 * 1000) * 24 * 60 - (diff / (60 * 60 * 1000) - diff
                / (24 * 60 * 60 * 1000) * 24) * 60);
        return returnMinute;

    }

    /**
     * 
     * 获取处理时长方法（精确到秒）
     * 
     * @author lzh
     * @param startDate
     *            开始时间
     * @param endDate
     *            结束时间
     * @return 时间间隔
     */
    public static long getTimeLongByOneDate(Date startDate, Date endDate)
    {

        if (startDate == null)
        {
            return 0;
        }
        if (startDate.getTime() > endDate.getTime())
        {
            return 0;
        }
        long millSeconds = (endDate.getTime() - startDate.getTime()); // 此处得到毫秒
        long seconds = millSeconds / 1000; // 此处为秒数
        return seconds;

    }
    
    /**
     * 
     * 获取处理时长方法（精确到天）
     * 
     * @author lzh
     * @param startDate
     *            开始时间
     * @param endDate
     *            结束时间
     * @return 时间间隔
     */
    public static long geDateLongByOneDate(Date startDate, Date endDate)
    {

        if (startDate == null)
        {
            return 0;
        }
        if (startDate.getTime() > endDate.getTime())
        {
            return 0;
        }
        long millSeconds = (endDate.getTime() - startDate.getTime()); // 此处得到毫秒
        long hours = millSeconds / 1000/60/60/24; // 此处为秒数
        return hours;

    }
    
    /**
     * Give a date, like 2012-03-19, will parse it to 2012-03-19 00:00:00
     * 
     * @param date
     * @return
     */
    public static Date beginOfADay(Date date) {
		return setCalendarTime(date, 0, 0, 0).getTime();
    }

    /**
     * Give a date, like 2012-03-19, will parse it to 2012-03-19 23:59:59
     * 
     * @param date
     * @return
     */
    public static Date endOfADay(Date date) {
		return setCalendarTime(date, 23, 59, 59).getTime();
    }
    
	public static Date changeTime(Date date, int hour, int min, int sec) {
		return setCalendarTime(date, hour, min, sec).getTime();
	}

	private static Calendar setCalendarTime(Date date, int hour, int min, int sec) {
		Calendar cal = Calendar.getInstance();
		if (null != date) {
			cal.setTime(date);
		}		
		cal.set(Calendar.HOUR_OF_DAY, hour);
		cal.set(Calendar.MINUTE, min);
		cal.set(Calendar.SECOND, sec);
		cal.set(Calendar.MILLISECOND, 0);
		return cal;
	}
	
	public static Date getTodayStart(int beforeDay) {
		Calendar c = Calendar.getInstance();
		int day = c.get(Calendar.DAY_OF_YEAR);
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		c.set(Calendar.DAY_OF_YEAR, day-beforeDay);
		return c.getTime();
	}
	
	public static Date getTodayEnd() {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		return c.getTime();
	}

}
